﻿using Microsoft.VisualBasic.Logging;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace COC_helper
{
    public partial class MapLeader : Form
    {
        public MapLeader()
        {
            InitializeComponent();
        }

        private void MapLeader_Shown(object sender, EventArgs e)
        {
            try
            {
                if (Global_Value.zmb_Setting.MapHandles == null)
                {
                    MessageBox.Show("地图数据是空的!但如果你刚刚建立这个项目,请忽略");
                    return;
                }
            }
            catch (Exception)
            {

                Global_Value.MyLogHandle.LogPush("地图数据类为空","Error");
            }
            try
            {
                foreach (var item in Global_Value.zmb_Setting.MapHandles)
                {

                    ListBox_MapList.Items.Add(item.Label);
                  
                }
            }
            catch (Exception)
            {

               // throw;
            }
            
        }

        private void MapLeader_FormClosing(object sender, FormClosingEventArgs e)
        {
            //MessageBox.Show("dhfougajvsw");
        }

        private void Button_Add_Click(object sender, EventArgs e)
        {
            if (TextBox_Label.Text == "")
            {
                MessageBox.Show("地图标签不可以为空");
                return;
            }
            else if (ListBox_MapList.Items.Contains(TextBox_Label.Text))
            {
                if (MessageBox.Show(TextBox_Label.Text + " 这个名字已经是另一个地图的了,要覆盖吗?", "地图预览器", MessageBoxButtons.YesNo) == DialogResult.Yes)
                {
                    Global_Value.zmb_Setting.MapHandles.Remove(Global_Value.zmb_Setting.MapHandles.Find(one => one.Label == TextBox_Label.Text));
                    ListBox_MapList.Items.Remove(TextBox_Label.Text);
                }
                else 
                {
                    return;
                }
                
            }
            TextBox_User.Text = TextBox_User.Text=="" ? Global_Value.zmb_Setting.Story_Name : TextBox_User.Text;
            if (pictureBox1.Image == null || TextBox_Path.Text == null)
            {
                MessageBox.Show("您的图呢?");
                return;
            }
            MapHandle mapHandle = new MapHandle();
            mapHandle.Label = TextBox_Label.Text;
            mapHandle.Tag = TextBox_Tag.Text;
            mapHandle.Belong = TextBox_Belong.Text;
            mapHandle.User = TextBox_User.Text;
            mapHandle.Path = TextBox_Path.Text;
            Global_Value.zmb_Setting.MapHandles.Add(mapHandle);
            ListBox_MapList.Items.Add(mapHandle.Label);



        }

        private void Button_New_Click(object sender, EventArgs e)
        {
            openFileDialog1.Title = "打开图片文件";
            openFileDialog1.Multiselect = false;
            openFileDialog1.InitialDirectory = Global_Value.ZMB_PATH;
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                TextBox_Path.Text = openFileDialog1.SafeFileName;
                try
                {
                    pictureBox1.Image = Image.FromFile(Global_Value.ZMB_PATH + openFileDialog1.SafeFileName);

                }
                catch (Exception err)
                {
                    Debug.WriteLine("图片加载失败了，因为" + err.ToString());
                   // throw;
                }
            }
        }

        private void ListBox_MapList_SelectedIndexChanged(object sender, EventArgs e)
        {
            if(ListBox_MapList.SelectedItem == null)
                { return; }
            try
            {
                var tempPath = Global_Value.zmb_Setting.MapHandles.Find(one => one.Label == ListBox_MapList.SelectedItem.ToString());
                if (tempPath == null)
                {
                    throw new Exception("一个列表里的值找不到对应的数据集");
                }
                TextBox_Label.Text = tempPath.Label;
                TextBox_Path.Text = tempPath.Path;
                TextBox_Tag.Text = tempPath.Tag;
                TextBox_User.Text = tempPath.User;
                TextBox_Belong.Text = tempPath.Belong;
                pictureBox1.Image = Image.FromFile(Global_Value.ZMB_PATH + tempPath.Path);
            }
            catch (Exception err)
            {
                Debug.WriteLine(err.ToString());
                Global_Value.MyLogHandle.LogPush(err.ToString(), "Error");
               // throw;
            }
        }

        private void Button_Del_Click(object sender, EventArgs e)
        {
            if(ListBox_MapList.SelectedItem == null || TextBox_Label.Text == null) { return; }
            Global_Value.zmb_Setting.MapHandles.Remove(Global_Value.zmb_Setting.MapHandles.Find(one => one.Label == TextBox_Label.Text));
            ListBox_MapList.Items.Remove(TextBox_Label.Text);
        }
    }
}
